package com.cz.nongyetongv5.activity;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.User;
import com.cz.nongyetongv5.custom.BlueLoadingProgress;
import com.cz.nongyetongv5.utils.ConfigUtil;
import com.cz.nongyetongv5.utils.GoHomeUtil;

public class RecordHistoryDetailActivity extends Activity {

	private User user;

	private ConfigUtil configUtil = null;

	private ViewGroup btnBack;

	private String url;

	private WebView wvWebView = null;

	private BlueLoadingProgress blpLoading = null;

	private TextView tvTitle = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.history_detail);

		tvTitle = (TextView) findViewById(R.id.tvTitle);

		if (!CommonConstants.isNetworkConnected(this)) {

			Toast.makeText(this,
					getResources().getString(R.string.error_no_network),
					Toast.LENGTH_SHORT).show();

			finish();

			return;
		}

		wvWebView = (WebView) findViewById(R.id.wvWebView);

		// 自动加载图片
		wvWebView.getSettings().setLoadsImagesAutomatically(true);
		// 启用脚本
		wvWebView.getSettings().setJavaScriptEnabled(true);

		wvWebView.addJavascriptInterface(new Object() {

			@JavascriptInterface
			public void showBigImg(String url) {

				String urlPre = configUtil.getPreHttpUrl();

				urlPre = urlPre.replace("/index.php/", "");

				String imgUrl = urlPre + url;

				Intent intent = new Intent(RecordHistoryDetailActivity.this,
						RecordHistoryPhotoDetailActivity.class);

				intent.putExtra("imgurl", imgUrl);

				startActivity(intent);

			}

		}, "androidapi");

		wvWebView.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageFinished(WebView view, String url) {

				if (blpLoading != null) {

					blpLoading.stopLoading();

					blpLoading.setVisibility(View.GONE);
				}

			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);

				if (blpLoading != null) {

					blpLoading.setVisibility(View.VISIBLE);
					blpLoading.startLoading();

				}
			}

		});

		blpLoading = (BlueLoadingProgress) findViewById(R.id.blpProgress);
		blpLoading.setVisibility(View.GONE);

		url = getIntent().getStringExtra("url");

		wvWebView.loadUrl(url);

		// 监督检查
		if (url.indexOf("check") > -1) {
			tvTitle.setText("监督检查详情");
		}

		// 产品检测
		if (url.indexOf("monitor") > -1) {
			tvTitle.setText("产品检测详情");
		}

		// 督查巡查
		if (url.indexOf("inspect") > -1) {
			tvTitle.setText("督查巡查详情");
		}

		user = (User) getIntent().getSerializableExtra("user");

		btnBack = (ViewGroup) findViewById(R.id.btnBack);

		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}

		});

		configUtil = new ConfigUtil(getResources());
		InitApplication initApplication = (InitApplication) getApplicationContext();
		initApplication.getActivities().add(this);

		loadRightTitle();

	}

	/**
	 * 加载去主页的按钮
	 */
	public void loadRightTitle() {

		ViewGroup btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		View view = GoHomeUtil.createPopView(getApplicationContext(),
				new String[] { "刷新", "主页" });

		// 弹出更多的菜单
		final PopupWindow pop = new PopupWindow(view,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);

		// 需要设置一下此参数，点击外边可消失
		pop.setBackgroundDrawable(new BitmapDrawable());

		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);

		// 设置此参数获得焦点，否则无法点击
		pop.setFocusable(true);

		final Button btn01 = (Button) pop.getContentView().findViewWithTag(
				"btn0");

		final Button btn02 = (Button) pop.getContentView().findViewWithTag(
				"btn1");

		OnClickListener menuOnClick = new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (v == btn01) {

					wvWebView.reload();

				} else if (v == btn02) {

					GoHomeUtil.goHome(getApplicationContext());
				}

				pop.dismiss();
			}
		};

		btn01.setOnClickListener(menuOnClick);

		btn02.setOnClickListener(menuOnClick);

		btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		btnMeanMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (pop.isShowing()) {
					// 隐藏窗口，如果设置了点击窗口外小时即不需要此方式隐藏
					pop.dismiss();
				} else {
					// 显示窗口
					pop.showAsDropDown(v);
				}

			}
		});

	}

}
